package cn.rongdao.jrkabao.ui.remind.uicomponent;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.dash_line.DashLineView;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.remind.RemindBean;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2017/11/21
 */

public class RemindDetailsUIComponent extends LinearLayout {
    public RemindDetailsUIComponent(Context context) {
        super(context);
    }

    public RemindDetailsUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RemindDetailsUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivAvatar;
    private TextView tvBankName;
    private TextView tvDate;
    private TextView tvDateLeft;
    private TextView tvName;
    private TextView tvRepayment;
    private TextView tvDateContent;
    private TextView tvRepaymentMoney;
    private TextView tvRestMoney;
    private TextView tvFree;
    private DashLineView dashLineView;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvName = (TextView) findViewById(R.id.ir_tv_name);
        tvRepayment = (TextView) findViewById(R.id.ir_tv_repayment);
        ivAvatar = (ImageView) findViewById(R.id.ir_iv_avatar);
        tvBankName = (TextView) findViewById(R.id.ir_tv_bank_name);
        tvDate = (TextView) findViewById(R.id.ir_tv_date);
        tvDateLeft = (TextView) findViewById(R.id.ir_tv_date_left);
        tvDateContent = (TextView) findViewById(R.id.ir_tv_date_content);
        tvRepaymentMoney = (TextView) findViewById(R.id.ir_tv_repayment_money);
        tvRestMoney = (TextView) findViewById(R.id.ir_tv_rest_money);
        tvFree = (TextView) findViewById(R.id.ir_tv_free);
        dashLineView = (DashLineView) findViewById(R.id.lrd_dash_view);
    }

    public void setGrayRemindBean(RemindBean.RemindBankList data) {
        setGrayStyle(data.getBalance(), data.getAmount_left(), data.getFree(), data.getButton_status(), data.getIs_export(), data.getBill_status());
        setBankInfo(data.getBank_icon(), data.getBank_name());
        setDateContent(data.getLeft_day(), data.getBill_status(), data.getText());
        setDashViewColor(ContextCompat.getColor(getContext(), R.color.c979797));
        setNameInfo(data.getUser_name(), data.getBankcard_no());
    }

    public void seWhiteRemindBean(RemindBean.RemindBankList data) {
        setWhiteStyle(data.getBalance_min(), data.getAmount_left(), data.getFree(), data.getButton_status());
        setDateContentWhite(data.getBalance(), data.getIs_export(), data.getBill_status());
        setDateWhite(data.getLeft_day(), data.getBill_status(), data.getText());
        setDashViewColor(Color.WHITE);
    }

    private void setBankInfo(String imageUrl, String bankName) {
        tvBankName.setText(FRString.valueOf(bankName));
        FRImageLoad.loadImage(FRString.valueOf(imageUrl), ivAvatar, R.drawable.icon_no_distinguish_bank);
    }

    private void setDateContent(String date, String billStatus, String text) {
        tvDate.setText(date.equals("0") ? "今" : FRString.valueOf(date));
        String left = getLeftText(date, billStatus);
        String content = FRCollection.appendAsString(left, "\n", text, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(14, true), 0, left.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        int textColor;
        if (billStatus.equals("2")) {
            textColor = R.color.cc7414c;
        } else {
            textColor = CommonUtil.isStatusSuccess(billStatus) ? R.color.cff7048 : R.color.c4164c7;
        }
        tvDate.setTextColor(ContextCompat.getColor(getContext(), textColor));
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), textColor)), 0, left.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvDateContent.setText(ss);
    }

    private void setNameInfo(String name, String bankCode) {
        String content = FRCollection.appendAsString(name, "  ", bankCode, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(13, true), name.length(), content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvName.setText(ss);
    }

    private void setGrayStyle(String repaymentMoney, String restMoney, String free, String buttonStatus, String isExport, String billStatus) {
        setRepaymentGreyBackGround(buttonStatus);
        if (!CommonUtil.isStatusSuccess(isExport)) {
            setGreyRepaymentCheckBalance(true);
        } else if (billStatus.equals("0")) {
            setGreyRepaymentCheckBalance(false);
        } else {
            setRepaymentMoney("应还金额", FRString.valueOf(repaymentMoney), ContextCompat.getColor(getContext(), R.color.c333333));
        }
        setRestMoney(FRString.valueOf(restMoney), ContextCompat.getColor(getContext(), R.color.c333333));
        setFree(FRString.valueOf(free), ContextCompat.getColor(getContext(), R.color.c333333));
    }

    private void setGreyRepaymentCheckBalance(boolean isExport) {
        String content;
        if (isExport) {
            content = "应还金额\n查余额>";
        } else {
            content = "应还金额\n未出账单";
        }
        tvRepaymentMoney.setTextColor(ContextCompat.getColor(getContext(), R.color.c333333));
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.cff7048)), 4, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvRepaymentMoney.setText(ss);
    }

    private void setRepaymentMoney(String text, String money, @ColorInt int colorRes) {
        String content = FRCollection.appendAsString(text, "\n", FRString.isEmpty(money) ? "-" : money, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(13, true), 4, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvRepaymentMoney.setTextColor(colorRes);
        tvRepaymentMoney.setText(ss);
    }

    private void setRestMoney(String money, @ColorInt int colorRes) {
        String content = FRCollection.appendAsString("剩余额度\n", FRString.isEmpty(money) ? "-" : money, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(13, true), 4, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvRestMoney.setTextColor(colorRes);
        tvRestMoney.setText(ss);
    }

    private void setFree(String free, @ColorInt int colorRes) {
        String content;
        if (FRString.isEmpty(free)) {
            content = FRCollection.appendAsString("免息\n", "-", "");
        } else {
            content = FRCollection.appendAsString("免息\n", free, "天", "");
        }
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(13, true), 2, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvFree.setTextColor(colorRes);
        tvFree.setText(ss);
    }

    private void setWhiteStyle(String repaymentMoney, String restMoney, String free, String buttonStatus) {
        tvRepayment.setBackgroundResource(R.drawable.btn_repayment_white);
        tvRepayment.setTextColor(Color.WHITE);
        tvRepayment.setVisibility(View.VISIBLE);
        if (buttonStatus.equals("2")) {
            tvRepayment.setText("立即更新");
        } else {
            tvRepayment.setText(CommonUtil.isStatusSuccess(buttonStatus) ? "已还清" : "立即还款");
        }
        setRepaymentMoney("最低应还", FRString.valueOf(repaymentMoney), Color.WHITE);

        setRestMoney(FRString.valueOf(restMoney), Color.WHITE);
        setFree(FRString.valueOf(free), Color.WHITE);
    }

    private void setDateContentWhite(String repaymentMoney, String isExport, String billStatus) {
        String content;
        boolean isBigTextSize = false;
        if (!CommonUtil.isStatusSuccess(isExport)) {
            content = "应还金额\n查余额>";
        } else if (billStatus.equals("0")) {
            content = "应还金额\n未出账单";
        } else {
            isBigTextSize = true;
            content = FRCollection.appendAsString("应还金额\n", FRString.isEmpty(repaymentMoney) ? "-" : repaymentMoney, "");
        }
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(12, true), 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(isBigTextSize ? new AbsoluteSizeSpan(24, true)
                        : new AbsoluteSizeSpan(14, true), 4, content.length()
                , Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvDateContent.setTextColor(Color.WHITE);
        tvDateContent.setText(ss);
    }

    private void setDateWhite(String date, String billStatus, String text) {
        ivAvatar.setVisibility(View.GONE);
        tvDateLeft.setVisibility(View.VISIBLE);
        tvDateLeft.setText(date.equals("0") ? "今" : FRString.valueOf(date));
        tvDate.setVisibility(View.GONE);
        tvBankName.setVisibility(View.GONE);
        String content = FRCollection.appendAsString(getLeftText(date, billStatus), "\n", text, "");
        tvName.setTextColor(Color.WHITE);
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(14, true), 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvName.setText(ss);
    }

    private String getLeftText(String date, String billStatus) {
        String left;
        if (billStatus.equals("2")) {
            left = date.equals("0") ? "天逾期" : "天已逾期";
        } else {
            left = CommonUtil.isStatusSuccess(billStatus) ? date.equals("0") ? "天还款" : "天内还款" : date.equals("0") ? "天出账" : "天后出账";
        }
        return left;
    }

    private void setRepaymentGreyBackGround(String buttonStatus) {
        tvRepayment.setVisibility(View.VISIBLE);
        if (buttonStatus.equals("2")) {
            tvRepayment.setText("立即更新");
            tvRepayment.setBackgroundResource(R.drawable.btn_repayment_orange);
            tvRepayment.setTextColor(Color.parseColor("#ff7048"));
        } else {
            tvRepayment.setText(CommonUtil.isStatusSuccess(buttonStatus) ? "已还清" : "立即还款");
            tvRepayment.setBackgroundResource(CommonUtil.isStatusSuccess(buttonStatus) ? R.drawable.btn_repayment_green : R.drawable.btn_repayment_red);
            tvRepayment.setTextColor(CommonUtil.isStatusSuccess(buttonStatus) ? Color.parseColor("#1b9b0b") : Color.parseColor("#ff4646"));
        }
    }

    private void setDashViewColor(@ColorInt int color) {
        dashLineView.setDashColor(color);
    }

    public void setRepaymentClickListener(OnClickListener onClickListener) {
        tvRepayment.setOnClickListener(onClickListener);
    }

    public void setCheckBalanceClickListener(OnClickListener onClickListener) {
        tvRepaymentMoney.setOnClickListener(onClickListener);
    }

    public void setWhiteCheckBalanceClickListener(OnClickListener onClickListener) {
        tvDateContent.setOnClickListener(onClickListener);
    }
}
